#!/usr/bin/env ruby

require 'rubygems'
require 'bio'

# Setup: make a random nucleotide sequence we can play with
nucs = ['a','t','g','c']
nucs += nucs.map{|xx| xx.upcase}
seq1Str = ""
numNucs = (rand(200) + 100)

numNucs.times { |xx|
  nucIndex = rand(nucs.size)
  theNuc = nucs[nucIndex]
  seq1Str << theNuc
}

puts "TEST SEQ:\n#{seq1Str}\n\n"

# Ok, now use BioRuby to get info about sequence, and manipulate the sequence for us
# Create NuclAcid sequence object:
bioNaSeq = Bio::Sequence::NA.new(seq1Str)
# Ask it the %GC and the agtc composition break down
puts "SEQ INFO:\n  %GC: #{bioNaSeq.gc_percent}\n  composition: #{bioNaSeq.composition.inspect}"
puts "TRANSLATED (3 frames):"
# Do a 3-frame translation
3.times { |ii|
  puts "  #{ii}. #{bioNaSeq.translate(ii+1)}"
}

puts "AA Composition of 1st frame translation:\n  #{bioNaSeq.translate.composition.inspect}"

# Determine molecular weight of first frame translation (upto first termination codon)
firstFrame = bioNaSeq.translate
if(firstFrame =~ /\*/)
  frameRange = 0...firstFrame.index("*")
  firstFrame = firstFrame[frameRange]
end
puts "Mol. Weight of 1st frame translation up to first termination codon:\n  #{firstFrame.molecular_weight}"